<template>
  <div>
    <el-header>
      <div class="sorts-container">
        <h1>类型筛选</h1>
        <div
          v-for="sort in sorts"
          :key="sort.id"
          class="sort-item"
          :class="{ 'active': selectedSortId === sort.id }"
          @click="updateCoupon(sort.id)"
        >
          {{ sort.sortName }}
        </div>
      </div>
    </el-header>
    <el-row style="margin-top: 20px; margin-left: 20px">
      <div style="display: flex">
        <el-tabs type="border-card" style="width: 30%">
          <el-tab-pane label="优惠券">
            <div v-for="coupon in couponData" :key="coupon.id" class="coupon">
              <div>
                <div class="discount">
                  {{ coupon.conditions }}元-{{ coupon.value }}元
                </div>
                <br />
                <div class="validity">有效期至: {{ coupon.endAt }}</div>
                <div class="details">{{ coupon.description }}</div>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      sorts: [],
      phone: "",
      userPhone: "",
      user: {},
      couponData: [],
      selectedSortId: null,
    };
  },
  methods: {
    //查用户优惠券
    queryCoupon() {
      this.$axios
        .get("api/zgyd-jifen/queryUsersCoupon/" + this.user.id)
        .then((res) => {
          if (res.data.code == 200) {
            this.couponData = res.data.data;


          }
        });
    },
    //查优惠券详情
    queryUser() {
      let param = new URLSearchParams();
      param.append("phone", this.userPhone);
      this.$axios.post("api/zgyd-user/queryUser", param).then((res) => {
        if (res.data.code == 200) {
          this.user = res.data.data;
          this.queryCoupon();
        }
      });
    },
    querySorts() {
      this.$axios.get("/api/zgyd-product/querySorts").then((res) => {
        if (res.data.code === 200) {
          this.sorts = res.data.data;
        }
      });
    },
    updateCoupon(val) {
      this.selectedSortId = val;
        this.$axios
        .get("api/zgyd-jifen/queryUsersCouponBySortId/" + this.user.id+"/"+val)
        .then((res) => {
          if (res.data.code == 200) {
            this.couponData = res.data.data;

          }
        });
      
     
    },
  },
  created() {
    this.userPhone = localStorage.getItem("username");
    this.queryUser();
    this.querySorts();
  },
};
</script>
<style scoped>
.centered-text-button {
  text-align: center;
  /* 水平居中 */
  line-height: 10px;
  /* 垂直居中 */
}

.coupon {
  width: 230px;
  height: 100px;
  background-color: #8b0000;
  /* 暗红色 */
  border: 2px dashed #ccc;
  border-radius: 10px;
  padding: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.coupon .left {
  flex: 1;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.sort-item.active {
  background-color: #8b0000; /* 暗红色 */
  color: #fff; /* 白色文字 */
}
.coupon .right {
  flex: 1;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.coupon .discount {
  font-size: 24px;
  color: #ff0000;
  /* 红色 */
  margin-right: 10px;
}

.coupon .details {
  font-size: 14px;
  color: #fff;
  /* 白色 */
}

.coupon .validity {
  font-size: 14px;
  color: #fff;
  /* 白色 */
}
.full-screen-mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5); /* 半透明黑色背景 */
  z-index: 9999; /* 确保遮罩位于其他内容之上 */
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>